package com.example.gkcircle.fragment;

import android.app.Fragment;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.FindCallback;
import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.holder.Holder;
import com.camnter.easyslidingtabs.widget.EasySlidingTabs;
import com.example.gkcircle.App;
import com.example.gkcircle.BaseFragment;
import com.example.gkcircle.R;
import com.example.gkcircle.activity.BookshelfActivity;
import com.example.gkcircle.activity.BoundLoginActivity;
import com.example.gkcircle.activity.LibraryBorrowActivity;
import com.example.gkcircle.activity.LoginActivity;
import com.example.gkcircle.activity.SearchBookActivity;
import com.example.gkcircle.adapter.TabsFragmentAdapter;
import com.example.gkcircle.library.LibraryRequest;
import com.example.gkcircle.library.object.GKUser;
import com.example.gkcircle.utils.ActivityUtil;
import com.example.gkcircle.utils.MySVProgressHUD;
import com.example.gkcircle.utils.ToastFactory;
import com.example.gkcircle.utils.UniversalImageLoader;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * 多读书Fragment
 * @author ZhouHua
 * created at 15/12/21 上午12:57
*/
public class LibraryFragment extends BaseFragment implements View.OnClickListener
{


	private LinearLayout mBorrowedLayout;
	private LinearLayout mShelfLayout;
	private LinearLayout mTopBookLayout;
    private ConvenientBanner mBanner;
	private LinearLayout mSearchBookLayout;
	private TextView mSearchBookBoxText;



	@Override
	protected int setLayout() {
		return R.layout.fragment_library;
	}

	@Override
	protected void initView(View view) {
		mSearchBookLayout=(LinearLayout)view.findViewById(R.id.home_lost_header_layout);
		mBorrowedLayout=(LinearLayout)view.findViewById(R.id.library_borrowed_layout);
		mShelfLayout=(LinearLayout)view.findViewById(R.id.library_shelf_layout);
		mTopBookLayout=(LinearLayout)view.findViewById(R.id.library_top_book_layout);
		mSearchBookBoxText=(TextView)view.findViewById(R.id.home_lost_header_text);
		mBanner=(ConvenientBanner)view.findViewById(R.id.library_banner);
	}

	@Override
	protected void initListener() {
		mSearchBookLayout.setOnClickListener(this);
		mBorrowedLayout.setOnClickListener(this);
		mShelfLayout.setOnClickListener(this);
		mTopBookLayout.setOnClickListener(this);
	}

	@Override
	protected void setData() {
		mSearchBookBoxText.setText(getResources().getString(R.string.library_search_book_box));
		//获取Banner地址
		AVQuery<AVObject> query = new AVQuery<>("Banner");
		query.setCachePolicy(AVQuery.CachePolicy.NETWORK_ELSE_CACHE);
		query.orderByDescending("createdAt");
		query.findInBackground(new FindCallback<AVObject>() {
			@Override
			public void done(List<AVObject> list, AVException e) {
				if (e == null) {
					List<String> imageList=new ArrayList<String>();
					for(int i=0;i<list.size();i++){
						imageList.add(list.get(i).getAVFile("img").getUrl());
					}
					mBanner.setPages(new CBViewHolderCreator<NetworkImageHolderView>() {
						@Override
						public NetworkImageHolderView createHolder() {
							return new NetworkImageHolderView();
						}
					},imageList).setPageIndicator(new int[]{R.mipmap.dot_normal,R.mipmap.dot_pressed});
				}
			}
		});

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()){
			case R.id.home_lost_header_layout:
				ActivityUtil.startActivity(getActivity(), SearchBookActivity.class);
				break;
			case R.id.library_borrowed_layout:
				AVUser user=AVUser.getCurrentUser();
				//先判断有没登录
				if(null==user){
					ToastFactory.getToast(getActivity(),getResources().getString(R.string.not_login));
					ActivityUtil.startActivity(getActivity(), LoginActivity.class);
					return;
				}
				//再判断是否有缓存图书馆账号信息
				if(null== GKUser.getCurrentUser(getActivity())){
					ActivityUtil.startActivity(getActivity(), BoundLoginActivity.class);
					return;
					//再判断cookie是否过期
				}else if(null== App.getCookie(1)){
					MySVProgressHUD.getInstance(getActivity()).showWithStatus(getResources().getString(R.string.logining));
					//登录账号拿cookie
					LibraryRequest.getInstance().autoLogin(getActivity(), new LibraryRequest.IAutoLoginListener() {
						@Override
						public void complete(GKUser user) {
							Message msg=new Message();
							msg.what=0;
							hanler.sendMessage(msg);
						}
					});
					return;
				}
				ActivityUtil.startActivity(getActivity(), LibraryBorrowActivity.class);
				break;
			case R.id.library_shelf_layout:
				ActivityUtil.startActivity(getActivity(), BookshelfActivity.class);
				break;
			case R.id.library_top_book_layout:
				break;
		}
	}
	private Handler hanler=new Handler(){
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what){
				case 0:
					MySVProgressHUD.getInstance(getActivity()).dismiss();
					ActivityUtil.startActivity(getActivity(), LibraryBorrowActivity.class);
					break;
			}
		}
	};

	public class NetworkImageHolderView implements Holder<String> {
		private ImageView imageView;
		@Override
		public View createView(Context context) {
			//你可以通过layout文件来创建，也可以像我一样用代码创建，不一定是Image，任何控件都可以进行翻页
			imageView = new ImageView(context);
			imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
			return imageView;
		}

		@Override
		public void UpdateUI(Context context,int position, String data) {
			UniversalImageLoader.getIntance().getInstance().displayImage(data,imageView);
		}
	}


	// 开始自动翻页
	@Override
	public void onResume() {
		super.onResume();
		//开始自动翻页
		mBanner.startTurning(5000);
	}

	// 停止自动翻页
	@Override
	public void onPause() {
		super.onPause();
		//停止翻页
		mBanner.stopTurning();
	}

}
